package com.hongyan.dao;

import com.hongyan.bean.Homework;
import com.hongyan.util.JDBCUtils;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class HomeworkDao {


    public static Object[][] findHomeworkByName(String name) {
        String sql = "select * from homework where h_title like '%" + name + "%'  order by h_id asc;";
        List<Homework> query = null;
        query = JDBCUtils.query(sql, Homework.class);
        Object[][] homeworks = new Object[query.size()][];
        for (int i = 0; i < query.size(); i++) {
            homeworks[i] = query.get(i).toArray();
        }
        return homeworks;

    }
    public static Object[][] findHomeworkByNameAndId(String name, String id) {
        String sql = "select * from homework where h_title like '%" + name + "%' and " + "c_id = '" + id + "' " + " order by h_id asc;";
        List<Homework> query = null;
        query = JDBCUtils.query(sql, Homework.class);
        Object[][] homeworks = new Object[query.size()][];
        for (int i = 0; i < query.size(); i++) {
            homeworks[i] = query.get(i).toArray();
        }
        return homeworks;

    }
    public static Object[][] getHomeworkListBySql(String sql) {
        System.out.println("sql = " + sql);
        List<Homework> query = null;
        query = JDBCUtils.query(sql, Homework.class);
        Object[][] homeworks = new Object[query.size()][];
        for (int i = 0; i < query.size(); i++) {
            homeworks[i] = query.get(i).toArray();
        }
        return homeworks;


    }

    public static int addHomework(Homework homework) {
        String sql = "INSERT INTO `javashixun`.`homework` (`h_id`, `c_id`, `h_title`, `content`, `start_time`, `end_time`, `t_grade`, `s_submission`) VALUES (?,?,?,?,?,?,?,?);";
        Object[] params = {homework.getH_id(), homework.getC_id(), homework.getH_title(), homework.getContent(), homework.getStart_time(), homework.getEnd_time(), homework.getT_grade(), homework.getS_submission()};
        int update = JDBCUtils.update(sql, params);
        return update;
    }

    public static int ScoringHomework(String h_id, double score) {
        String sql = "UPDATE `javashixun`.`homework` SET `t_grade` = ? WHERE (`h_id` = ?);";
        Object[] params = {score, h_id};
        int update = JDBCUtils.update(sql, params);
        return update;
    }

    public static int AnswerHomework(String h_id, String Answer) {
        String sql = "UPDATE `javashixun`.`homework` SET `s_submission` = ? WHERE (`h_id` = ?);";
        Object[] params = {Answer, h_id};
        int update = JDBCUtils.update(sql, params);
        return update;
    }
}
